<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属科室" prop="userDept">
        <el-input
          v-model="queryParams.userDept"
          placeholder="请选择所属部门"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="deptPhone">
        <el-input
          v-model="queryParams.postName"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     <!-- <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="可用状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:post:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
        >删除
        </el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-lock"
          size="mini"
          :disabled="single"
          @click="assignRoles"
          v-hasPermi="['system:post:export']"
        >分配角色
        </el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleReset"
          v-hasPermi="['system:post:export']"
        >重置密码
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户ID" align="center" prop="userId"/>
      <el-table-column label="用户姓名" align="center" prop="userName"/>
      <el-table-column label="部门" align="center" prop="dept_name"/>
      <el-table-column label="手机号码【登陆身份】" align="center" prop="userPhone"/>
      <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat"/>
      <el-table-column label="年龄" align="center" prop="userAge"/>
      <el-table-column label="是否排班" align="center" prop="userJourney" :formatter="journeyFormat"/>
      <el-table-column label="级别" align="center" prop="level_name"/>
      <el-table-column label="背景" align="center" prop="edu_name"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名称"/>
        </el-form-item>
        <el-form-item label="手机号" prop="userPhone">
          <el-input v-model="form.userPhone" placeholder="请输入手机号，作为登录凭证"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属科室">
              <el-select v-model="form.userDept" placeholder="请选择" prop="userDept">
                <el-option
                  v-for="item in deptOptions"
                  :key="item.deptId"
                  :label="item.deptName"
                  :value="item.deptId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="背景">
              <el-select v-model="form.userEducation" placeholder="请选择" prop="userEducation">
                <el-option
                  v-for="item in eduOptions"
                  :key="item.eduId"
                  :label="item.eduName"
                  :value="item.eduId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="级别">
              <el-select v-model="form.userLevel" placeholder="请选择" prop="userLevel">
                <el-option
                  v-for="item in levelOptions"
                  :key="item.levelId"
                  :label="item.levelName"
                  :value="item.levelId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="年龄" prop="userAge">
          <el-input-number v-model="form.userAge" controls-position="right" :min="0"/>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio
              v-for="dict in sexOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否参与排班" prop="userJourney">
          <el-radio-group v-model="form.userJourney">
            <el-radio
              v-for="dict in journeyOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 分配角色 -->
    <el-dialog :title="title" :visible.sync="open2" width="1000px" append-to-body>
      <el-form ref="form2" :model="form2" :rules="rules" label-width="100px">

        <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange2">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="ID" align="center" prop="roleId"/>
          <el-table-column label="角色名称" align="center" prop="roleName"/>
          <el-table-column label="权限编码" align="center" prop="roleKey"/>
          <el-table-column label="备注" align="center" prop="remark"/>
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
        </el-table>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRole">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import {listPost, getPost, delPost, addUser, updateUser, exportPost,resetUserPwd,addUserRole} from "@/api/syl_Sxl/user";
    import {listDept} from "@/api/syl_Sxl/dept"
    import {listEducation} from "@/api/syl_Sxl/education"
    import {listLevel} from "@/api/syl_Sxl/level"


    export default {
        name: "Post",
        data() {
            return {

                timeOptions:[],

                deptOptions:[],

                eduOptions:[],

                levelOptions:[],

                roleList:[],



                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 岗位表格数据
                postList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                open2:false,
                // 状态数据字典
                statusOptions: [],
                // 性别状态字典
                sexOptions: [],
                journeyOptions:[],


                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    postCode: undefined,
                    postName: undefined,
                    status: undefined,
                    userJourney:undefined
                },
                // 表单参数
                form: {},
                form2:{},
                // 表单校验
                rules: {
                    userName: [
                        {required: true, message: "用户名称不能为空", trigger: "blur"}
                    ],
                    userPhone: [
                        {required: true, message: "手机号码不能为空", trigger: "blur"}
                    ]
                }
            };
        },
        created() {
            this.getList();
            this.getDicts("sys_normal_disable").then(response => {
                this.statusOptions = response.data;
            });
            this.getDicts("sys_user_sex").then(response => {
                this.sexOptions = response.data;
            });
            this.getDicts("his_user_journey").then(response => {
                this.journeyOptions = response.data;
            });
        },
        methods: {
            /** 查询岗位列表 */
            getList() {
                this.loading = true;
                listPost(this.queryParams).then(response => {
                    this.postList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 岗位状态字典翻译
            statusFormat(row, column) {
                return this.selectDictLabel(this.statusOptions, row.status);
            },
            // 性别字典翻译
            sexFormat(row, column) {
                return this.selectDictLabel(this.sexOptions, row.sex);
            },
            journeyFormat(row, column){
                return this.selectDictLabel(this.journeyOptions, row.userJourney);
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.open2=false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    postId: undefined,
                    postCode: undefined,
                    postName: undefined,
                    postSort: 0,
                    status: "0",
                    userJourney:"0",
                    remark: undefined
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.userId)
                this.single = selection.length != 1
                this.multiple = !selection.length
            },

            // 多选框选中数据
            handleSelectionChange2(selection) {
                this.ids = selection.map(item => item.roleId)
                this.single = selection.length != 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加用户";
                listDept().then(response =>{
                    this.deptOptions = response.data;
                });
                listEducation().then(response =>{
                    this.eduOptions = response.data;
                });
                listLevel().then(response =>{
                    this.levelOptions = response.data;
                });

            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const postId = row.userId || this.ids
                getPost(postId).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改员工";
                });
                listDept().then(response =>{
                    this.deptOptions = response.data;
                });
                listEducation().then(response =>{
                    this.eduOptions = response.data;
                });
                listLevel().then(response =>{
                    this.levelOptions = response.data;
                });
            },
            /** 提交按钮 */
            submitForm: function () {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.userId != undefined) {
                            updateUser(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addUser(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            //添加角色确定按钮
            submitRole:function(){


                                this.msgSuccess("添加成功");
                                this.open2 = false;

            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const postIds = row.userId || this.ids;
                this.$confirm('是否确认删除用户编号为"' + postIds + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return delPost(postIds);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
            },
            /** 导出按钮操作 */
            handleExport() {
                const queryParams = this.queryParams;
                this.$confirm('是否确认导出所有岗位数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return exportPost(queryParams);
                }).then(response => {
                    this.download(response.msg);
                })
            },
            /** 修改密码 */
            handleReset(row) {

                const postIds = row.userId || this.ids;
                this.$prompt('请输入"' + postIds + '"的新密码', "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消"
                }).then(({value}) => {
                    resetUserPwd(postIds[0], value).then(response => {
                        this.msgSuccess("修改成功，新密码是：" + value);
                    });
                }).catch(() => {
                });
            },
            /*****角色查询********/
            assignRoles(row) {

                this.reset();
                const postId = row.userId || this.ids
                listRole(postId).then(response => {
                    this.form2 = response.data;
                    this.roleList = response.rows;
                    this.open2 = true;
                    this.title = "分配角色";
                });

                /*this.reset();
                this.open2 = true;
                this.title = "添加角色";
                const postId = row.userId || this.ids
                listRole(this.queryParams).then(response => {
                    this.userId=postId;
                    this.roleList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });*/
            }

        }
    };
</script>
